package com.example.myanimator.mcanvas

import android.graphics.*
import android.graphics.drawable.Drawable
import android.util.Log

/**
 *
 */
class RoundImgDrawable(private val mBitmap: Bitmap) : Drawable() {
    private val mPaint: Paint
    private var bitmapShader: BitmapShader? = null
    private var rectF: RectF? = null
    override fun setBounds(left: Int, top: Int, right: Int, bottom: Int) {
        super.setBounds(left, top, right, bottom)
        bitmapShader = BitmapShader(Bitmap.createScaledBitmap(mBitmap, right - left, bottom - top, true), Shader.TileMode.CLAMP,
                Shader.TileMode.CLAMP)
        mPaint.shader = bitmapShader
        rectF = RectF(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat())
        Log.e("tgw", "left:$left ,$top  ,$right  ,$bottom")
    }

    override fun getIntrinsicWidth(): Int {
        return mBitmap.width
    }

    override fun getIntrinsicHeight(): Int {
        return mBitmap.height * 2
    }

    override fun draw(canvas: Canvas) {
        canvas.drawRoundRect(rectF!!, 20f, 20f, mPaint)
    }

    override fun setAlpha(alpha: Int) {
        mPaint.alpha = alpha
    }

    override fun setColorFilter(cf: ColorFilter?) {
        mPaint.colorFilter = cf
    }

    override fun getOpacity(): Int {
        Log.e("tgw", "getOpacity")
        return PixelFormat.OPAQUE
    }

    init {
        mPaint = Paint()
        mPaint.setARGB(20, 0xff, 0x00, 0x00)
        mPaint.isAntiAlias = true
    }
}